package com.dragon.www.haier.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.dragon.www.haier.R;
import com.dragon.www.haier.entity.ServiceResult;
import com.dragon.www.haier.net.NetHelper;
import com.dragon.www.haier.net.NetUtils;


/**
 * Created by daiye on 2016/5/31.
 */
public class RegisterActivity extends FragmentActivity {
    private EditText mMailEt = null;
    private EditText mPasswordEt = null;
    private EditText mPasswordAgainEt = null;
    private Button mRegisterBt = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void initData(){

    }

    private void initView() {
        setContentView(R.layout.activity_register);
        mMailEt = (EditText)findViewById(R.id.mail_et);
        mPasswordEt = (EditText)findViewById(R.id.password_et);
        mPasswordAgainEt = (EditText)findViewById(R.id.password_again_et);
        mRegisterBt = (Button)findViewById(R.id.register_user_bt);
        mRegisterBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                submit();
            }
        });
    }

    private void submit(){
        String mailStr = mMailEt.getText().toString();
        String passwordStr = mPasswordEt.getText().toString();
        String passwordAgainStr = mPasswordAgainEt.getText().toString();
        if(TextUtils.isEmpty(mailStr) ){
            Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if(TextUtils.isEmpty(passwordStr) ){
            Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if(TextUtils.isEmpty(passwordAgainStr) ){
            Toast.makeText(this, "第二次输入的密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        if(!passwordStr.equals(passwordAgainStr)){
            Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
            return;
        }
        if(passwordStr.length() < 6 ){
            Toast.makeText(this, "密码长度不能低于6位", Toast.LENGTH_SHORT).show();
            return;
        }

        // 用户注册请求
        NetHelper.userRegister(this, mailStr, passwordStr, new NetUtils.NetCallBack<ServiceResult>() {
            @Override
            public void success(ServiceResult rspData) {
                if (rspData.getCode().equals("0000")) {
                    Intent intent = new Intent();
                    setResult(RESULT_OK, intent);
                    RegisterActivity.this.finish();
                }
            }

            @Override
            public void failed(String msg) {
                Toast.makeText(RegisterActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        }, ServiceResult.class);
    }
}
